package com.selfdriving.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class DeviceUtils {
	public static void callPhone(Context context, String num) {
		if (num.trim().length() != 0) {
			Intent phoneIntent = new Intent("android.intent.action.CALL",
					Uri.parse("tel:" + num));
			context.startActivity(phoneIntent);
		}
	}
	
	public static void sendSMS(Context context, String sms) {
		if (sms.trim().length() != 0) {
			Uri smsToUri = Uri.parse("smsto:");
			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
			intent.putExtra("sms_body", sms);
			context.startActivity(intent);
		}
	}
	
	public static void sendMail(Context context, String sms) {
		String[] reciver = new String[] { "181712000@qq.com" };  
        String[] mySbuject = new String[] { "test" };  
        String myCc = "cc";  
        String mybody = "测试Email Intent";  
        Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);  
        myIntent.setType("plain/text");  
        myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);  
        myIntent.putExtra(android.content.Intent.EXTRA_CC, myCc);  
        myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);  
        myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);  
        context.startActivity(Intent.createChooser(myIntent, "mail test")); 
	}
	
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}
}
